package com.y.mytest;

import com.y.mytest.jm.aes.AESUtils;
import com.y.mytest.jm.ZlibTest;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class QEncodeUtil {
    public static void main(String[] args) throws Exception {
        String charname = "UTF-8";
        byte[] content = new byte[]{120, -100, 51, 52, 50, 54, 49, 53, 51, -73, -80, 4, 0, 9, 30, 1, -34};
        byte[] ens2 = AESUtils.encrypt2(content);
        System.out.println("" + Arrays.toString(ens2));

        String t = new String(ens2);
        System.out.println(t);

        String s = new String(t.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1);
        System.out.println("tos：" + s);
        byte[] bytes = s.getBytes();
        System.out.println("" + Arrays.toString(bytes));
        /*byte[] b = new byte[]{49, 50, 51, 52, 53, 54, 55, 56, 57};
        String bytes = new String(b);*/

        //System.out.println("可以转换的进制范围：" + Character.MIN_RADIX + "-" + Character.MAX_RADIX);
        //System.out.println("2进制：" + binary(bytes, 2));
        //System.out.println("5进制：" + binary(bytes, 5));
        System.out.println("8进制：" + binary(bytes, 8));
        System.out.println("10进制：" + binary(bytes, 10));
        System.out.println("16进制：" + binary(bytes, 16));
        System.out.println("32进制：" + binary(bytes, 32));
        System.out.println("64进制：" + binary(bytes, 64));// 这个已经超出范围，超出范围后变为10进制显示

        System.out.println(Arrays.toString(hexToByteArray(binary(bytes, 16))));
        System.out.println(hexStringToString(binary(bytes, 16)));
        System.out.println(Arrays.toString(hexStringToString(binary(bytes, 16)).getBytes(StandardCharsets.ISO_8859_1)));

        byte[] decompressData = ZlibTest.decompress(content);
        System.out.println("解压缩后的字节流：" + Arrays.toString(decompressData));
    }

    /**
     * 将byte[]转为各种进制的字符串
     *
     * @param bytes byte[]
     * @param radix 基数可以转换进制的范围，从Character.MIN_RADIX到Character.MAX_RADIX，超出范围后变为10进制
     * @return 转换后的字符串
     */
    public static String binary(byte[] bytes, int radix) {
        return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
    }

    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, StandardCharsets.UTF_8);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    public static byte[] hexToByteArray(String inHex) {
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1) {
            //奇数
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            //偶数
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = hexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }

    public static byte hexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }
}